﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Forecast
{
    public partial class VisitForecast : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PagerControll.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            if (!this.IsPostBack)
            {
                InitControlResource();
                PagerControll_PageChange(0);
            }
        }

        protected void gvForecast_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                Model.VisitRequest visitRequest = e.Row.DataItem as Model.VisitRequest;
                if (visitRequest != null)
                {
                    Model.Request request = RequestPresent.GetById(visitRequest.RequestId);
                    var visitType1 = VisitTypePresent.GetByCondition(g => g.Id == visitRequest.VisitType1);
                    var visitType2 = VisitTypePresent.GetByCondition(g => g.Id == visitRequest.VisitType2);
                    int i = 0;
                    e.Row.Cells[i++].Text = (e.Row.RowIndex + 1 + (PagerControll.CurrentPageIndex) * PagerControll.PageSize).ToString();
                    e.Row.Cells[i++].Text = string.Format("{0:yyyy-MM-dd}", request.RequestDate);
                    e.Row.Cells[i++].Text = request.StartTime.ToString().Substring(0, 5) + "~" + request.EndTime.ToString().Substring(0, 5);
                    if (Language == Language.CN)
                    {
                        e.Row.Cells[i++].Text = request.Lab.Location.Name_CN;
                        e.Row.Cells[8].Text = visitType1.Name_CN + "&nbsp;&nbsp;" + visitType2.Name_CN;
                    }
                    else
                    {
                        e.Row.Cells[i++].Text = request.Lab.Location.Name_EN;
                        e.Row.Cells[8].Text = visitType1.Name_EN + "&nbsp;&nbsp;" + visitType2.Name_EN;
                    }
                    e.Row.Cells[i++].Text = request.Lab.Name;

                    //todo   LabManager可能会有问题，刘永峰会继续关注解决
                    var labManager = LabManagerPresent.GetByCondition(q => q.LabId == request.LabId);

                    if (labManager != null)
                    {
                        var labRole = UserRolePresent.GetList(g => g.RoleId == labManager.RoleId);
                        if (labRole.Count != 0)
                        {
                            StringBuilder linkMan = new StringBuilder();
                            foreach (var labMan in labRole)
                            {
                                linkMan.Append(labMan.User.NameEN + "、");
                            }
                            e.Row.Cells[i++].Text = linkMan.ToString().Substring(0, linkMan.Length - 1);
                        }
                    }
                }
            }
        }

        protected void Select_Command(object sender, EventArgs e)
        {
            //todo 需求没有明确
            int index = this.gvForecast.SelectedIndex;
            int visitRequestId = int.Parse(gvForecast.DataKeys[index].Value.ToString());
            int requsetId = VisitRequestPresent.GetById(visitRequestId).RequestId;
            Response.Redirect("/Request/Apply.aspx?pagestatus=Readonly&read=true&id=" + requsetId);
        }

        public override void InitControlResource()
        {
            int i = 0;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.No;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_Date;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_Time;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Location_Name;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Lab_Name;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_Linkman;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_CompanyName;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_PersonNum;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.VisitRequest_VisitorType;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.VisitRequest_Purpose;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_MainPersonName;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_MainPersonTitle;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Visit_AccompanyManager;
            gvForecast.Columns[i++].HeaderText = Resources.NameResource.Action;
        }

        void PagerControll_PageChange(int pageIndex)
        {
            var visitRequest = VisitRequestPresent.GetPaged(pageIndex, PagerControll.PageSize, GetLambaCondition(), g => g.Request.RequestDate,true);
            gvForecast.DataSource = visitRequest;
            gvForecast.DataBind();
            PagerControll.IntialProperties(visitRequest.TotalCount);
        }

        private Expression<Func<Model.VisitRequest, bool>> GetLambaCondition()
        {
            DateTime sevenDay = DateTime.Today.AddDays(7);
            Expression<Func<Model.VisitRequest, bool>> searchExp = (p => (DateTime.Compare(p.Request.RequestDate, DateTime.Today) >= 0)
                && (DateTime.Compare(p.Request.RequestDate, sevenDay) <= 0) && (p.Request.Status == (int)RequestStatus.Approved));

            return searchExp;
        }
    }
}